package br.com.hugo.senai.testes;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
 
import javax.swing.ImageIcon;
 
public class CopyImageToClipboard {
 
    public static void main(String[] args) throws InterruptedException {
     
        String start = "C:\\Users\\Hugo\\Pictures\\Camera Roll\\1.jpg";
        File folder = new File(start);
        File folderOld = new File(start + "old");
        folderOld.mkdir();
     
        while(true){
            String [] files = folder.list();
            for (int i = 0; i < files.length; i++) {
                if( files[i].endsWith(".jpg")){
                    System.out.println("copy file : " + files[i]);
                    ImageIcon icon = new ImageIcon(start + files[i]);
                    setClipboard( icon.getImage());
                    File image = new File(start + files[i]);
                    image.renameTo( new File(start + "old/" + files[i]));
                    break;
                }
                 
            }
            Thread.sleep(300);
        }
    }
 
    public static void setClipboard(Image image) {
        ImageSelection imgSel = new ImageSelection(image);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
    }
 
    // This class is used to hold an image while on the clipboard.
    public static class ImageSelection implements Transferable {
        private Image image;
 
        public ImageSelection(Image image) {
            this.image = image;
        }
 
        // Returns supported flavors
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[] { DataFlavor.imageFlavor };
        }
 
        // Returns true if flavor is supported
        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return DataFlavor.imageFlavor.equals(flavor);
        }
 
        // Returns image
        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            if (!DataFlavor.imageFlavor.equals(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            return image;
        }
    }
 
}